package com.washmadam.service.common.base;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.washmadam.service.db.dto.base.BaseTable;

import common.service.base.common.MobileSession;
import common.service.base.common.SessionInfo;
import common.service.contants.MobileConfig;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @author Administrator
 *
 */
@Slf4j
public abstract class BaseController {

    @Autowired
    MobileSession mobileSession;

    private static final Long TIME_HOUR = 1000 * 60 * 60l;//请求超时时长;

    /**
     * @param @return 设定文件
     * @return Long 返回类型
     * @throws @author lixin
     * @Title: getUserId
     * @Description: 获取用户id
     * @date 2018年9月7日 上午10:40:10
     */
    public Long getSessionUserId() {
        try {
            SessionInfo sessionInfo = (SessionInfo) mobileSession.getAttribute(MobileConfig.SESSION_USR_INFO);
            if (sessionInfo != null) {
                return sessionInfo.getUserId();
            }
        } catch (Exception e) {
            log.error("【登陆session】 获取用户ID，异常，{}", e);
        }
        return null;
    }

    public String getSessionUserCode() {
//        try {
//            SessionInfo sessionInfo = (SessionInfo) mobileSession.getAttribute(MobileConfig.SESSION_USR_INFO);
//            if (sessionInfo != null) {
//                return sessionInfo.getCode();
//            }
//        } catch (Exception e) {
//            logger.error("【登陆session】 获取用户code，异常，{}", e);
//        }
        return null;
    }

    public SessionInfo getSessionUser() {
        try {
            SessionInfo sessionInfo = (SessionInfo) mobileSession.getAttribute(MobileConfig.SESSION_USR_INFO);
            if (sessionInfo != null) {
                return sessionInfo;
            }
        } catch (Exception e) {
            log.error("【登陆session】 获取用户code，异常，{}", e);
        }
        return null;
    }
    
    public void  buildCreateAndUpdateInfo(BaseTable baseTable){
    	SessionInfo sessionInfo = getSessionUser();
    	if(baseTable.getId() == null) {
    		baseTable.setCreateTime(new Date());
    		baseTable.setCreateBy(sessionInfo==null?null:sessionInfo.getUserId());
    	}
    	baseTable.setUpdateBy(sessionInfo==null?null:sessionInfo.getUserId());
    	baseTable.setUpdateTime(new Date());
    }
}
